package com.dropdb.demo.controller.admin;

import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.AdminConfigService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author:cheyanjun
 * @date:2020/06/26 20:50
 */

@RestController
@RequestMapping("admin/config")
public class AdminConfigController {

    @Autowired
    AdminConfigService adminConfigService;

    /**
     * 商场管理
     *
     * @return
     */
    @GetMapping("mall")
    @RequiresPermissions(value = {"*", "admin:config:mall:list"}, logical = Logical.OR)
    public BaseRespVO getMall() {
        Map malldata = adminConfigService.getMall();
        return BaseRespVO.ok(malldata);
    }

    /**
     * 商场修改
     *
     * @param malldata
     * @return
     */
    @PostMapping("mall")
    @RequiresPermissions(value = {"*", "admin:config:mall:updateConfigs"}, logical = Logical.OR)
    public BaseRespVO postMall(@RequestBody Map malldata) {
        for (Object value:malldata.values()){
            if (value==null){
                return BaseRespVO.error(402, "数据不能为空");
            }
        }
        String phone = (String) malldata.get("cskaoyan_mall_mall_phone");
        if (phone.length() != 11) {
            return BaseRespVO.error(402, "请输入正确的电话号码");
        }
        String qq = (String) malldata.get("cskaoyan_mall_mall_qq");
        if (qq.length() < 9) {
            return BaseRespVO.error(402, "请输入正确的qq号码");
        }
        adminConfigService.postMall(malldata);
        return BaseRespVO.ok();
    }

    /**
     * 运费管理
     *
     * @return
     */
    @GetMapping("express")
    @RequiresPermissions(value = {"*", "admin:config:express:list"}, logical = Logical.OR)
    public BaseRespVO getExpress() {
        Map expressdata = adminConfigService.getExpress();
        return BaseRespVO.ok(expressdata);
    }

    /**
     * 运费修改
     *
     * @param expressdata
     * @return
     */
    @PostMapping("express")
    @RequiresPermissions(value = {"*", "admin:config:express:updateConfigs"}, logical = Logical.OR)
    public BaseRespVO postExpress(@RequestBody Map expressdata) {
        for (Object value:expressdata.values()){
            if (value==null){
                return BaseRespVO.error(402, "数据不能为空");
            }
            int express = Integer.parseInt(value.toString());
            if(express<0){
                return BaseRespVO.error(402, "请输入正确的数据");
            }
        }
        adminConfigService.postExpress(expressdata);
        return BaseRespVO.ok();
    }

    /**
     * 订单管理
     *
     * @return
     */
    @GetMapping("order")
    @RequiresPermissions(value = {"*", "admin:config:order:list"}, logical = Logical.OR)
    public BaseRespVO getOrder() {
        Map orderdata = adminConfigService.getOrder();
        return BaseRespVO.ok(orderdata);
    }

    /**
     * 订单修改
     *
     * @param orderdata
     * @return
     */
    @PostMapping("order")
    @RequiresPermissions(value = {"*", "admin:config:order:updateConfigs"}, logical = Logical.OR)
    public BaseRespVO postOrder(@RequestBody Map orderdata) {
        for(Object value :orderdata.values()){
            if (value==null){
                return BaseRespVO.error(402, "数据不能为空");
            }
            int time = Integer.parseInt(value.toString());
            if(time<0){
                return BaseRespVO.error(402, "请输入正确的时间");
            }
        }
        adminConfigService.postOrder(orderdata);
        return BaseRespVO.ok();
    }

    /***
     * 小程序管理
     * @return
     */
    @GetMapping("wx")
    @RequiresPermissions(value = {"*", "admin:config:wx:list"}, logical = Logical.OR)
    public BaseRespVO getWx() {
        Map wxdata = adminConfigService.getWx();
        return BaseRespVO.ok(wxdata);
    }

    /***
     * 小程序修改
     * @return
     */
    @PostMapping("wx")
    @RequiresPermissions(value = {"*", "admin:config:wx:updateConfigs"}, logical = Logical.OR)
    public BaseRespVO postWx(@RequestBody Map map) {
        for (Object value:map.values()){
            if (value==null){
                return BaseRespVO.error(402, "数据不能为空");
            }
            int data = Integer.parseInt(value.toString());
            if(data<0){
                return BaseRespVO.error(402, "请输入正确的数据");
            }
        }
        adminConfigService.postWx(map);
        return BaseRespVO.ok();
    }

}
